﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework.Constraints;
using NUnit.Framework;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

using SAKS.Models;

namespace SAKS.UnitTest.ModelsTest
{
    [TestFixture]
    public class MessageModelTest : TestBase
    {

        protected override void CreateSomeData(ISession session)
        {
            var from = new User()
            {
                UserName = "From",
                LoweredUserName = "From",
                IsAnonymous = false,
                LastActivityDate = DateTime.Now
            };
            var to = new User()
            {
                UserName = "To",
                LoweredUserName = "to",
                IsAnonymous = false,
                LastActivityDate = DateTime.Now
            };
            session.Save(to);
            session.Save(from);
            session.Flush();
        }

        #region message
        [Test]
        public void AddAndDeleteMessageSentTest()
        {
            var message = new MessageSent()
            {
                Date = DateTime.Now,
                Subject = "AddMessageSentTest",
                HasRead = false,
            };

            using (var session = sessionFactory.OpenSession())
            {
                var from = session.QueryOver<User>().Where(u => u.UserName == "From").SingleOrDefault();
                var to = session.QueryOver<User>().Where(u => u.UserName == "To").SingleOrDefault();
                message.ToName = to.UserName;
                message.FromName = from.UserName;
                Guid id = (Guid)session.Save(message);
                session.Flush();
                Assert.NotNull(session.QueryOver<MessageSent>().Where(m => m.Id == id).SingleOrDefault());

                session.Delete(message);
                session.Flush();
                Assert.IsNull(session.QueryOver<MessageSent>().Where(m => m.Id == id).SingleOrDefault());
            }
        }
        [Test]
        public void AddAndDeleteMessageReceivedTest()
        {
            var message = new MessageReceived()
            {
                Date = DateTime.Now,
                Subject = "AddMessageSentTest",
                HasRead = false,
            };

            using (var session = sessionFactory.OpenSession())
            {
                var from = session.QueryOver<User>().Where(u => u.UserName == "From").SingleOrDefault();
                var to = session.QueryOver<User>().Where(u => u.UserName == "To").SingleOrDefault();
                message.ToName = "To";
                message.FromName = "From";
                Guid id = (Guid)session.Save(message);
                session.Flush();
                Assert.NotNull(session.QueryOver<MessageReceived>().Where(m => m.Id == id).SingleOrDefault());

                session.Delete(message);
                session.Flush();
                Assert.IsNull(session.QueryOver<MessageReceived>().Where(m => m.Id == id).SingleOrDefault());
            }
        }

        #endregion
    }
}
